﻿/*
    ExternallCall.as
    Copyright (c) Otávio Ribeiro<otavio@otavio.eng.br> @ 2008
    
    This file is part of OtaSwfUploader application.

    Birthday is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Birthday is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package
{
	import flash.external.ExternalInterface;
	
	import UploadFile;
	
	class ExternalCall
	{
		private var movieName:String;
		
		/* CALLBACKS */
		private var PENDING_FILE_ADDED_CALLBACK:String;
		private var UPLOAD_NOTIFY_CALLBACK:String;
		private var INIT_CALLBACK:String;
		
		public function ExternalCall(mn:String)
		{
			this.movieName = mn;			
			this._createCallbacks();
		}
		
		private function _createCallbacks():void
		{
			this.INIT_CALLBACK = "OtaSwfUploader.instances['"+this.movieName+"'].onInitPlugin";
			this.PENDING_FILE_ADDED_CALLBACK = "OtaSwfUploader.instances['"+this.movieName+"'].onPendingFileAdded";
			this.UPLOAD_NOTIFY_CALLBACK = "OtaSwfUploader.instances['"+this.movieName+"'].onUploadNotify";
		}
		
		public static function dispatchExternalOnPendingFileAdded(callback:String,movieName:String, info:Object)
		{
			ExternalInterface.call(callback,movieName,info);
		}
		
		public function dispatchOnPendingFileAdded(file:UploadFile):void
		{
			ExternalCall.dispatchExternalOnPendingFileAdded(this.PENDING_FILE_ADDED_CALLBACK,this.movieName,file.toJsObject());
		}
		
		public static function dispatchExternalOnUploadNotify(callback:String, pos:Number, status:Number, info:Object)
		{
			ExternalInterface.call(callback,pos,status,info);
		}
		
		public function dispatchOnUploadNotify(pos:Number, status:Number, info:Object)
		{
			ExternalCall.dispatchExternalOnUploadNotify(this.UPLOAD_NOTIFY_CALLBACK,pos,status,info);
		}
		
		public static function dispatchExternalOnInitPlugin(callback:String)
		{
			ExternalInterface.call(callback);
		}
		
		public function dispatchOnInitPlugin()
		{
			ExternalCall.dispatchExternalOnInitPlugin(this.INIT_CALLBACK);
		}
	}
}
